<?php
/**
 +------------------------------------------------------------------------------
 * framework 入口文件
 +------------------------------------------------------------------------------
 * @Version   1.9
 * @Author    WangXian
 * @E-mail     admin@loopx.cn
 * @FileName index.php
 * @Creation date 2010-9-8
 * @Modified date 2010-10-11
 +------------------------------------------------------------------------------
 */
if(!defined('APP_PATH')) exit;

#系统运行效率标记
$run_start_time = microtime(1);			#启动时间
$run_start_memory = memory_get_usage();	#内存使用大小
$run_dbquery_count = 0;					#系统运行中，执行了多少次QUERY

#定义系统常量
define("URL", $_SERVER['SCRIPT_NAME']);	#带入口文件index.php,例如:/book/index.php
include FW_PATH.'/common/functions_base.php';#加载公共方法，

#获取main系统配置
$config = C();

#时区 & session 设置
date_default_timezone_set($config['date_timezone']);

#显示系统错误
if($config['debug'] == true)
{
	ini_set('display_errors', $config['display_errors']);
	error_reporting($config['error_reporting']);
}


#是否启用session, open_session = true的时候干预系统session的设置，否则采用php.ini默认设置
if ( $config['session']['open_session'] )
{
	#重设置session参数
	if( $config['session']['reset'])
	{
		#如果服务器设置auto_start = 1，则先注销当前session
		if( ini_get('session.auto_start') ) session_destroy();
		
		ini_set('session.name', $config['session']['name']);
		ini_set('session.use_cookies', $config['session']['use_cookies']);
		ini_set('session.use_only_cookies', $config['session']['use_only_cookies']);
		ini_set('session.use_trans_sid', $config['session']['use_trans_sid']);
		if(! empty($_GET[ $config['session']['name'] ])) session_id( $_GET[ $config['session']['name'] ] );
		session_start();
	}
	else if(! ini_get('session.auto_start') ) session_start();
}


#加载核心类
include FW_PATH.'/exception/ephpexception.class.php';
include FW_PATH.'/core/view.class.php';
include FW_PATH.'/core/app.class.php';
include FW_PATH.'/core/controller.class.php';
include FW_PATH.'/core/model.class.php';

#加载数据库驱动
if( empty($config['dbdriver']) ) $config['dbdriver'] = 'mysqli';
$driver_name = FW_PATH.'/db/'. $config['dbdriver'] .'.class.php';
try{
	if(file_exists($driver_name))
		include $driver_name;
	else
		throw new ephpException('错误：配置文件main.config.php中使用了不支持的驱动<font color="red"> '. $config['dbdriver'] .'</font> 请使用：mysqli、mysql、sqlite 。',1013);
}
catch (ephpException $e)
{
	exit($e);
}
	
#类自动加载控制
function __autoload($classname)
{
	//dump($classname);
	#加载模型
	if(substr($classname,-5) == 'Model')
	{
		if(file_exists(APP_PATH.'/models/'.$classname.'.php'))
		{ require APP_PATH.'/models/'.$classname.'.php';}
		else
		{ throw new ephpException(APP_PATH.'/models/'.$classname.'.php 文件不存在!',1737);}
		
	}#加载控制器
	elseif(substr($classname,-10) == 'Controller')
	{
		if(file_exists(APP_PATH.'/controllers/'.$classname.'.php'))
		{ require APP_PATH.'/controllers/'.$classname.'.php';}
		else
		{
			if(! C('debug'))
			show404();
			else
			throw new ephpException($classname.'没有定义，可能原因controllers/'.$classname.'.php不存在！',1738);
		}
		
	}
	else
	{#加载第三方库，位置在ext/下
		if( file_exists(APP_PATH.'/exts/'.$classname.'.class.php') )
		{ include APP_PATH.'/exts/'.$classname.'.class.php'; }
		elseif( file_exists(FW_PATH.'/exts/'.$classname.'.class.php') )
		{ include FW_PATH.'/exts/'.$classname.'.class.php'; }
		else
		{ throw new ephpException(APP_PATH.'/exts/'.$classname.'.class.php 文件不存在!',1739); }
	}
	return 0;
}

#设置error\exception异常处理函数
function error_handler($errno, $errstr, $errfile, $errline )
{
	global $config;
	#是否显示notice异常，NOTICE_PRINT = true时显示，false不显示，直接返回
	if( ($errno == E_NOTICE) && ( $config['notice_print'] == true ) ) return 0;
	
	#避免E_STRICT错误,stdClass不初始化就会有错误的严格检查
	//if( $errno == E_STRICT ) return 0;
    
	# 抛出系统异常
	throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
set_error_handler("error_handler");


#记录系统访问日志
if($config['access_log'])
{
	$str="\nREQUEST_URL:http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."\n";
	$str.='REMOTE_ADDR:'.$_SERVER['REMOTE_ADDR']."\n";
	$str.='HTTP_USER_AGENT:'.$_SERVER['HTTP_USER_AGENT']."\n";
	$str.='REMOTE_ADDR:'.$_SERVER['REMOTE_ADDR']."\n";
	$str.='------------------------------';
	wlog('AccessLog-'.date('Y-m-d').'.log', '['. date('H:i:s') .']'.$str);
}

//加载其他文件
//...